package exia.java.core.services;

import java.util.List;

import net.sf.hibernate.Session;
import exia.java.core.entities.Utilisateur;

class HibernateServiceAuthentificationImpl implements
		IServiceAuthentification {

	private static final String REMOTE_SERVICE_NAME = "Hibernate Remote Authentification Service";
	private static final double REMOTE_SERVICE_VERSION = 0.1;
	private Session WORKING_SESSION;
	private final IServiceUtilisateur serviceUtilisateur = RemoteServiceFactory.getServiceUtilisateur();
	
	
	/**
	 * 
	 */
	public HibernateServiceAuthentificationImpl(){
		super();
	}
	
	public Utilisateur authentifier(final String login, final String password) throws ServiceAuthentificationException {
		Utilisateur utilisateur = null;
		if((login != null)){
			if((password != null)){
				if(serviceUtilisateur != null){
					
					List<Utilisateur> users;
					try {
						users = serviceUtilisateur.getUtilisateurs();
						if((users != null)&&(users.size() > 0)){
							for(Utilisateur u : users){
								if((u.getLogin().equalsIgnoreCase(login))&&(u.getPassword().equalsIgnoreCase(password))){
									utilisateur = u;
									break;
								}
							}
						}
					} catch (ServiceUtilisateurException ex) {
						ex.printStackTrace();
					}
					
				}
			}else{
				throw new ServiceAuthentificationException("NULL POINTER ERROR: The password parameter of the authentifier method is null.");
			}
		}else{
			throw new ServiceAuthentificationException("NULL POINTER ERROR: The login parameter of the authentifier method is null.");
		}
		return utilisateur;
	}

	public String getRemoteServiceName() {
		return REMOTE_SERVICE_NAME;
	}

	public double getRemoveServiceVersion() {
		return REMOTE_SERVICE_VERSION;
	}

	public Object performRemoteServiceFunction(String functionName, Object[] params) throws RemoteServiceException {
		// TODO Auto-generated method stub
		return null;
	}

}
